﻿using System;
using System.IO;
using Douyu.SDK;
using System.Text;
using System.Threading.Tasks;
using System.Threading;
using Microsoft.Data.Sqlite;
using Dapper;
using System.Linq;
using System.Collections.Generic;
using Newtonsoft.Json;

namespace dmget
{

    class Program
    {
        static void Main(string[] args)
        {
            Console.OutputEncoding = Encoding.Unicode;
            DouyuSocketClient a = new DouyuSocketClient();
            a.ConnectAsync().Wait();
            a.Login(1651548);
            a.JoinGroup();
            a.KeepLive();
            a.OnReceiveKeepLiveMessage += (x, r) => { Console.WriteLine("KeepLive..."); };
            a.OnReceiveChatMessage += (x, v) =>
            {
                Console.WriteLine(v.txt.IndexOf(".cha"));
                if (v.txt.IndexOf(".cha") == 0)
                {
                    var select = v.txt.Substring(4);
                    using (SqliteConnection s = new SqliteConnection("DataSource=item.db"))
                    {
                        var re = s.Query<itemModel>($"select * from item where name like '%{select}%'").FirstOrDefault();
                        if (re != null)
                        {
                            var arr = JsonConvert.DeserializeObject<string[]>(re.get);
                            StringBuilder sb = new StringBuilder();
                            foreach( var i in arr)
                            {
                                sb.Append(i);
                                sb.Append(",");
                            }
                            sb.Remove(sb.Length - 1, 1);
                            File.WriteAllText(@"C:\Users\Cherrs\Documents\test.txt", $"物品名：{re.name}\r\n类型：{re.type}\r\n描述：{re.title}\r\n获取途径：{sb.ToString()}");
                        }
                    }
                    
                }

                
            };
            Console.ReadLine();
        }
    }
}